//
//  CNGoodsDisplayView.m
//  BeeQuick_19
//
//  Created by tanky on 2016/9/24.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNGoodsDisplayView.h"
#import "CNCenterLineLabel.h"
#import "CNHomeTableViewController.h"

@implementation CNGoodsDisplayView

- (void)setModel:(CNBestSellerModel *)model{
    if (_model != model) {
        _model = model;
        [self setupUI];
    }
}

#pragma mark 加载界面及数据
- (void)setupUI{
        CGFloat screenWidth =  [UIScreen mainScreen].bounds.size.width;
        CGFloat maxMargin = 8;
        CGFloat margin = 5;
        CGFloat minMargin = 2;
    
        CNHomeTableViewController *homeVC = [self getCurrentViewController];
    
        /** 宝贝图片*/
        UIImageView *goodsImgView = [[UIImageView alloc] init];
        //返回的数据 链接内有空格需删除
        NSString *urlString = [self.model.img stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSString *newURLString = [urlString stringByReplacingOccurrencesOfString:@"," withString:@""];
        [goodsImgView sd_setImageWithURL:[NSURL URLWithString:newURLString] placeholderImage:[UIImage imageNamed:@"v2_placeholder_half_size"]];
        [self addSubview:goodsImgView];
        [goodsImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset((screenWidth - 3 * maxMargin) / 2 - minMargin);
            make.left.top.equalTo(self);
        }];
        self.goodsImgView = goodsImgView;
        /** 宝贝名称*/
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.text = self.model.name;
        nameLabel.font = [UIFont systemFontOfSize:18];
    
        [self addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(goodsImgView.mas_bottom).offset(minMargin);
            make.left.offset(margin);
            make.right.offset(margin);
        }];
        /** 精选*/
        UIImageView *bestCollectionImgView = [[UIImageView alloc] init];
        bestCollectionImgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bestCollectionImgView];
        [bestCollectionImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(nameLabel.mas_bottom).offset(minMargin);
            make.left.offset(margin);
            make.width.equalTo(@60);
            make.height.equalTo(@24.5);
        }];
        /** 买一赠一*/
        UIImageView *buyOneGetOneImgView = [[UIImageView alloc] init];
        buyOneGetOneImgView.image = [UIImage imageNamed:@"buyOne.png"];
        buyOneGetOneImgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:buyOneGetOneImgView];
        [buyOneGetOneImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bestCollectionImgView);
            make.left.equalTo(bestCollectionImgView.mas_right);
            make.width.equalTo(@60);
            make.height.equalTo(@25.5);
        }];
        /** 商品规格*/
        UILabel *specLabel = [[UILabel alloc] init];
        specLabel.text = self.model.specifics;
        specLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:specLabel];
        [specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(bestCollectionImgView.mas_bottom).offset(minMargin);
            make.left.offset(margin);
        }];
        /** 现价*/
        UILabel *currentPriceLabel = [[UILabel alloc] init];
        currentPriceLabel.text = [NSString stringWithFormat:@"￥%@",self.model.partner_price];
        currentPriceLabel.font = [UIFont systemFontOfSize:15];
        currentPriceLabel.textColor = [UIColor redColor];
        [self addSubview:currentPriceLabel];
        [currentPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(specLabel.mas_bottom).offset(minMargin);
            make.left.offset(margin);
        }];
        /** 原价*/
        CNCenterLineLabel *originalPriceLabel = [[CNCenterLineLabel alloc] init];
        originalPriceLabel.text = [NSString stringWithFormat:@"￥%@",self.model.market_price];
        originalPriceLabel.font = [UIFont systemFontOfSize:13];
        originalPriceLabel.textColor = [UIColor darkGrayColor];
        [self addSubview:originalPriceLabel];
        [originalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(currentPriceLabel);
            make.left.equalTo(currentPriceLabel.mas_right);
        }];
        /** 加入购物车按钮*/
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [addBtn setImage:[UIImage imageNamed:@"v2_increase"] forState:UIControlStateNormal];
        [addBtn sizeToFit];
        addBtn.alpha = .7;
        [self addSubview:addBtn];
        [addBtn addTarget:homeVC action:@selector(addButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-margin);
            make.bottom.equalTo(self).offset(-maxMargin);
        }];
    
        /** 商品数量*/
        UILabel *countLabel = [[UILabel alloc] init];
        countLabel.text = [NSString stringWithFormat:@"%zd",self.model.count];
        countLabel.font = [UIFont systemFontOfSize:15];
        [countLabel sizeToFit];
        [self addSubview:countLabel];
        [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(addBtn);
            make.right.equalTo(addBtn.mas_left).offset(-minMargin);
        }];
        self.countLabel = countLabel;
    
        /** 删除购物车*/
        UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [minusBtn setImage:[UIImage imageNamed:@"v2_reduce"] forState:UIControlStateNormal];
        [minusBtn sizeToFit];
        minusBtn.alpha = .7;
        [self addSubview:minusBtn];
        [minusBtn addTarget:homeVC action:@selector(minuesButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(countLabel);
            make.right.equalTo(countLabel.mas_left).offset(-minMargin);
        }];
        self.minuesBtn = minusBtn;
    
        /** 这里需要判断是否显示减按钮*/
    if (self.model.count == 0) {
        countLabel.text = [NSString stringWithFormat:@"%zd",self.model.count];
        minusBtn.alpha = 0;
        countLabel.alpha = 0;
    }else{
        minusBtn.alpha = 1;
        countLabel.alpha = 1;
        countLabel.text = [NSString stringWithFormat:@"%zd",self.model.count];
    }
        self.backgroundColor = [UIColor whiteColor];
    
    if ([self.model.pm_desc isEqualToString:@"买一赠一"]) {
        buyOneGetOneImgView.alpha = 1;
    }else{
        buyOneGetOneImgView.alpha = 0;
    }
    
    if (self.model.hot_degree == 0) {
        bestCollectionImgView.image = [UIImage new];
    }else{
        bestCollectionImgView.image = [UIImage imageNamed:@"jingxuan.png"];
    }
}

/** 获取当前View的控制器对象 */
- (CNHomeTableViewController *)getCurrentViewController{
    UIResponder *next = [self nextResponder];
    do {
        if ([next isKindOfClass:[CNHomeTableViewController class]]) {
            return (CNHomeTableViewController *)next;
        }
        next = [next nextResponder];
    } while (next != nil);
    return nil;
}

@end
